from research.calcor.pos import original_close
class stop_close(original_close):
    def __init__(self,argtype="percent",name="",istick=True,stop_loss="stop_loss",stop_gain="stop_gain",atrkey=""):
        super().__init__(argtype,name,istick,stop_loss,stop_gain,atrkey)

class stop_close_bytick:
    def __init__(self):
        self.stop_loss=None
        self.stop_gain=None
    def reset(self):
        self.stop_loss = None
        self.stop_gain = None
    def set_stop_prices(self,stop_loss=None,stop_gain=None):
        self.stop_loss = stop_loss
        self.stop_gain = stop_gain
    def onbar(self,ss,b):
        if ss.pos=="buy":
            p0=self.stop_loss
            isclose = False
            if p0 is not None:
                if b["low"]<p0 and b["high"]>p0:
                    price=p0
                    isclose=True
                    msg="触发止损价" + str(p0)
                elif b["low"]<p0 and b["high"]<p0:
                    price=b["open"]
                    isclose=True
                    msg = "跳开止损价" + str(p0)+",open"+str(price)
                if isclose:
                    order = {"obj": b["obj"], "timekey": b["timekey"], "price": price, "action": "closesell",
                             "info": msg}
                    ss.signals_data.append(order)
                    ss.pos = None
                    self.reset()
                    return True,"closesell","stop_loss"
            p0=self.stop_gain
            if p0 is not None:
                if b["low"]<p0 and b["high"]>p0:
                    price=p0
                    isclose=True
                    msg = "触发止赢价" + str(p0)
                elif b["low"]>p0 and b["high"]>p0:
                    price=b["open"]
                    isclose=True
                    msg = "跳开止赢价" + str(p0) + ",open" + str(price)
                if isclose:
                    order = {"obj": b["obj"], "timekey": b["timekey"], "price": price, "action": "closesell",
                             "info": msg}
                    ss.signals_data.append(order)
                    ss.pos = None
                    self.reset()
                    return True,"closesell","stop_gain"
        elif ss.pos=="sell":
            p0 = self.stop_loss
            isclose=False
            if p0   is not None:
                if b["low"] < p0 and b["high"] > p0:
                    price = p0
                    isclose = True
                    msg = "触发止损价" + str(p0)
                elif b["low"] > p0 and b["high"] > p0:
                    price = b["open"]
                    isclose = True
                    msg = "跳开止损价" + str(p0) + ",open" + str(price)
                if isclose:
                    order = {"obj": b["obj"], "timekey": b["timekey"], "price": price, "action": "closebuy",
                             "info": msg}
                    ss.signals_data.append(order)
                    ss.pos = None
                    self.reset()
                    return True, "closebuy","stop_loss"
            p0 = self.stop_gain
            if p0   is not None:
                if b["low"]<p0 and b["high"]>p0:
                    price=p0
                    isclose = True
                    msg = "触发止赢价" + str(p0)
                elif b["low"]<p0 and b["high"]<p0:
                    price=b["open"]
                    isclose = True
                    msg = "跳开止赢价" + str(p0) + ",open" + str(price)
                if isclose:
                    order = {"obj": b["obj"], "timekey": b["timekey"], "price": price, "action": "closebuy",
                             "info": msg}
                    ss.signals_data.append(order)
                    ss.pos = None
                    self.reset()
                    return True,"closebuy", "stop_gain"
        return False,"",""